﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Super.DomainService;
using Super.Entities;
using Super.Infrastructure;
using Super.Mvc;

namespace Super.Web.Areas.Admin.Controllers
{
    public class InterfaceTypeController : BaseController
    {
        //
        // GET: /Admin/InterfaceType/

        public ActionResult Index()
        {
            var page = Query.Params("Page");
            if (string.IsNullOrEmpty(page))
            {
                page = "1";
            }
            var adp = new DbContextAdapter();
            var interfaceTypeService = new InterfaceTypeService(adp);
            var interfaceList =
                interfaceTypeService.Repository.Find(a => a.IsDel == false).ToList();
          
            return View(interfaceList);

        }

        public ActionResult SubSearch()
        {
            var adp = new DbContextAdapter();
            var interfaceTypeService = new InterfaceTypeService(adp);
            var interfaceList =
                interfaceTypeService.Repository.Find(a => a.IsDel == false).ToList();

            return View(interfaceList);
        }

        public ActionResult Edit()
        {
            var id = Query.Get("id").ToInt(0);
            if (id > 0)
            {
                var adp = new DbContextAdapter();
                var interfaceTypeService = new InterfaceTypeService(adp);
                var interfaceModel = interfaceTypeService.Repository.Find(a => a.ID == id).FirstOrDefault();
                return View(interfaceModel);
            }
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Edit([Bind(Prefix = "")]InterfaceType model)
        {
            var adp = new DbContextAdapter();
            var interfaceTypeService = new InterfaceTypeService(adp);
            if(model.ID<=0)
            {
                model.CreateTime = DateTime.Now;
            }
            interfaceTypeService.Repository.InsertOrUpdate(model);
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage);

        }

        public JsonResult Delete()
        {
            var pid = Query.Get("id").ToInt(0);
            var adp = new DbContextAdapter();
            var interfaceService = new InterfaceTypeService(adp);
            var interfaceModel = interfaceService.Repository.Find(a => a.ID == pid).FirstOrDefault();

            if(interfaceModel==null)
            {
                return JsonBack.Tip("出现异常！未找到实体！");
            }

            if(interfaceModel.ProductInterfaceType.Count>0)
            {
                return JsonBack.Tip("该接口类型被使用，不能删除！", "interfaceType");
            }
            else
            {
                interfaceService.Repository.Delete(interfaceModel, true);
            }
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "interfaceType", "删除成功");
        }
    }
}
